/*=======================================================================================
	SummaryStatistics.do

		Computes summary statistics for the Personal Finance Website (PFW) data 

	Author: Lorenz Kueng, June 2015
=========================================================================================*/

cap log close PFW_03
log using "$homedir/log-files/PFW_03_$date.log", text replace name(PFW_03)


*=============================================================
* Data 
*=============================================================

use "$homedir/data/stata/PFW_quarterly.dta", clear

	generate date = Quarter

	xtset userid Quarter

	*-----------------------------------------------------------------
	* Apply same sample selection as in main regression analysis
	*-----------------------------------------------------------------

		** Winsorize nondurables and services at 1%

			cap drop temp
			winsor   nondur_serv, generate(nondur_serv_W) p(0.01) 
			replace  nondur_serv_W = 0 if nondur_serv_W==.

		** Run baseline regression to select same estimation sample 

			reghdfe D.nondur_serv_W APFD ///
				if APFDid_annual!=. & liquid_asset1!=. ///
				, absorb(date Alaska) vce(cluster userid)
			drop nondur_serv_W

			keep if e(sample)



*=============================================================
* Summary Statistics
*=============================================================

	*----------------------------------
	* Calculate additional variables
	*----------------------------------

		** Other expenditures in total expenditures not contained in nondurables or smaller durables
		
		generate other_totexp = totexp5 - nondur_serv - durable 
	
	
		** Add PFD to regular income

		replace  APFD_annual = 0 if state=="WA" // set to zero (before it was imputed based on self-reported family sizes to serve as a 'placebo' dividend)
		replace  APFD_annual = . if year==2014  
		
		generate incomeY_incl_PFD = incomeY + APFD_annual // income after deduction and tax withholding, inclusive of PFD

		

	*----------------------------------
	* Winsorize nominal variables
	*----------------------------------

		foreach var in nondur_serv durable other_totexp totexp5 incomeY incomeY_incl_PFD bank_balance brokerage ret_savings  /// main variables
					   child_supp otherexp debt_pay mortgage_rent cash_withdrawals uncategorized user_defined check_payments /// additonal categories in 'other items in total expenditures'
					{
			winsor   `var', generate(`var'W) p(0.01)
		}


	*----------------------------------
	* Relative size of dividend
	*----------------------------------

		replace APFD_annual =. if state=="WA"

		* by annual income

		generate APFDcurrW = APFD_annual / incomeY_incl_PFDW * 100 


		* by annual total expenditures

		bysort userid: egen totexp_annual_nominal = mean(totexp1_nominal) // average quarterly total spending: totexp1 = nondur + service + durable + child_supp + otherexp + debt_pay + mortgage_rent
		replace  totexp_annual_nominal = totexp_annual_nominal*4
		replace  totexp_annual_nominal = -totexp_annual_nominal // annualized total expenditures
		winsor totexp_annual_nominal, p(0.01) generate(totexp_annual_nominalW)

		generate ShockSize = APFD_annual_nominal / totexp_annual_nominalW * 100
		winsor ShockSize, generate(ShockSizeW) p(0.01)


	*----------------------------------
	* Cash-on-hand ratio
	*----------------------------------

		* by annual income

		generate cash_on_handYW = bank_balanceW/ (incomeY_incl_PFDW/4) * 100 


		* by quarterly total expenditures

		bysort userid: egen totexp_annual = mean(totexp1) 
		replace  totexp_annual = totexp_annual*4
		replace  totexp_annual = -totexp_annual
		winsor totexp_annual, p(0.01) generate(totexp_annualW)

		generate cash_on_handEW = bank_balanceW / (totexp_annualW/4) * 100 

		
	*----------------------------------
	* Tabulate summary statistics
	*----------------------------------

	cap log close PFW_SumStat
	log using "$homedir/results/tables/Table1_SummaryStats_PFW_${date}.log", text replace name(PFW_SumStat)

		***Dividend and Income 

			//first collapse annual variables to houseohld level so that each household has the same weight in the sample average
			preserve	
				sort userid date
				collapse (mean)        APFD_annual APFDcurrW ShockSizeW incomeYW incomeY_incl_PFDW   bank_balanceW durableW other_totexpW totexp5W, by(Alaska userid year)
				bysort Alaska: tabstat APFD_annual APFDcurrW ShockSizeW incomeYW incomeY_incl_PFDW, statistics(N mean median sd) columns(statistics) noseparator varwidth(32)


					************************************************************************************
					***Income and liquid assets by after-tax income quintiles for buffer stock model
					************************************************************************************
					
					gen incomeY_quarterly = incomeY_incl_PFDW/4 // quarterly 'residual' income available for nondurable purchases
					xtile income_Q = totexp5W if Alaska==1, nquantiles(5) 
					gen liquidity_ratio1 = bank_balanceW/totexp5W
					gen liquidity_ratio2 = bank_balanceW/incomeY_quarterly
					bysort income_Q: tabstat liquidity_ratio* , statistics(mean median) columns(statistics) noseparator varwidth(32)

					tabstat liquidity_ratio* if Alaska==1, statistics(mean median) columns(statistics) noseparator varwidth(32)

					di 40903/24576     // liquidity ratio 1 based on Table 1 = 1.6643473
					di 40903/(99716/4) // liquidity ratio 2 based on Table 1 = 1.6407798
			restore


			
		***Expenditures

			bysort Alaska: tabstat 	nondur_servW durableW other_totexpW totexp5W ///
									child_supp otherexp debt_pay mortgage_rent cash_withdrawals uncategorized user_defined check_payments ///
								, statistics(N mean median sd) columns(statistics) noseparator varwidth(16)	                          


		***Demographics and Financial Assets

			generate total_assetsW = bank_balanceW + brokerageW + ret_savingsW 

			//first collapse demographics to houseohld level so that each household has the same weight in the sample average
			preserve
				sort userid date
				collapse (mean)        family_size age edu bank_balanceW cash_on_handEW cash_on_handYW brokerageW ret_savingsW total_assetsW, by(Alaska userid)

				bysort Alaska: tabstat family_size age edu bank_balanceW cash_on_handEW cash_on_handYW brokerageW ret_savingsW total_assetsW, statistics(N mean median sd) columns(statistics) noseparator varwidth(32)
			restore

		***Number of households and observations by state
		
			tabulate state
			unique userid if Alaska==1
			unique userid if Alaska==0

			
			
	cap log close PFW_SumStat

log close PFW_03
 
